package frontend.parser.syntaxtree;

import frontend.lexer.TKType;
import frontend.parser.syntaxtree.symbol.NonTerminalSymbol;
import frontend.parser.syntaxtree.symbol.TerminalSymbol;

import java.util.function.Consumer;

public class CharacterNode extends SyntaxNode {

    public String charConst;

    @Override
    public void walk(Consumer<TerminalSymbol> terConsumer, Consumer<NonTerminalSymbol> nonTerConsumer) {
        terConsumer.accept(new TerminalSymbol(TKType.CHRCON, charConst));
        nonTerConsumer.accept(new NonTerminalSymbol(this));
    }

    @Override
    public String getType() {
        return "Character";
    }

    public Integer getCharConstValue() {
        if (charConst.length() == 3) {
            return (int) charConst.charAt(1);
        }
        //其他情况
        switch (charConst.charAt(2)) {
            case 'a' -> {
                return 7;
            }
            case 'b' -> {
                return 8;
            }
            case 't' -> {
                return 9;
            }
            case 'n' -> {
                return 10;
            }
            case 'v' -> {
                return 11;
            }
            case 'f' -> {
                return 12;
            }
            case '"' -> {
                return 34;
            }
            case '\'' -> {
                return 39;
            }
            case '\\' -> {
                return 92;
            }
            case '0' -> {
                return 0;
            }
        }
        return 0;
    }
}